package com.qianfeng.admin.controller;

import com.qianfeng.entity.dto.AdminModuleRole;
import com.qianfeng.entity.dto.AdminRole;
import com.qianfeng.entity.po.LoginAdmin;
import com.qianfeng.http.Result;
import com.qianfeng.service.impl.AdminModuleRoleService;
import com.qianfeng.service.impl.AdminModuleService;
import com.qianfeng.service.impl.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private AdminRoleService adminRoleService;
    @Autowired
    private AdminModuleRoleService adminModuleRoleService;
    @Autowired
    private AdminModuleService adminModuleService;


    @RequestMapping("list")
    public String list(Model model , LoginAdmin loginAdmin){
        model.addAttribute("roleList" , adminRoleService.list());
        model.addAttribute("adminModuleList" , adminModuleService.getByAdminId(loginAdmin.getId()));
        return "permission/list";
    }

    @RequestMapping("getById")
    @ResponseBody
    public Result<?> getById(@RequestParam(value = "id" , required = true) Long roleId){
        return Result.success(new RoleData(adminRoleService.getById(roleId), adminModuleRoleService.getByRoleId(roleId)));
    }

    @RequestMapping("save")
    @ResponseBody
    public Result<?> save(AdminRole role , @RequestParam(value = "moduleRoles" , required = true) String moduleRoles){
        adminModuleRoleService.saveRole(role , moduleRoles);
        return Result.success();
    }

    @RequestMapping("del")
    @ResponseBody
    public Result<?> del(@RequestParam(value = "id" , required = true) Long roleId){
        adminRoleService.removeById(roleId);
        adminModuleRoleService.removeByRoleId(roleId);
        return Result.success();
    }





    class RoleData{
        private AdminRole adminRole;
        private List<AdminModuleRole> adminModuleRoleList;

        public RoleData() {
        }

        public RoleData(AdminRole adminRole, List<AdminModuleRole> adminModuleRoleList) {
            this.adminRole = adminRole;
            this.adminModuleRoleList = adminModuleRoleList;
        }

        public AdminRole getAdminRole() {
            return adminRole;
        }

        public void setAdminRole(AdminRole adminRole) {
            this.adminRole = adminRole;
        }

        public List<AdminModuleRole> getAdminModuleRoleList() {
            return adminModuleRoleList;
        }

        public void setAdminModuleRoleList(List<AdminModuleRole> adminModuleRoleList) {
            this.adminModuleRoleList = adminModuleRoleList;
        }
    }


}
